package tt.dz.egobus.servicemanager.business.convert;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.point.BusPoint;

import java.util.Arrays;
import java.util.List;

/**
 * @author Anson
 * @date 17-8-10
 * @name 从缓存中过滤出内部直营司机
 * @since 1.0.6
 */
public class FilterBusInnerPointConverter implements Converter<BusinessCache, BusPoint[]> {

    @Override
    public BusPoint[] convert(BusinessCache source) {
        List<String> driverIds = source.convetDriverIds();
        if (CollectionUtils.isEmpty(driverIds)) {
            return new BusPoint[0];
        }
        //过滤直营司机
        List<String> innerDriverIds = this.filterInnerDriver(driverIds);
        if (CollectionUtils.isEmpty(innerDriverIds)) {
            //若不存在直营司机的时候
            return new BusPoint[0];
        }
        BusPoint[] points = source.getDrivers();
        points = this.matcherDriverIds(innerDriverIds, points);
        return points;
    }

    /**
     * @param driverIds 司机集合
     * @author Anson
     * @date 17-8-10
     * @name 获取集合中的直营司机
     * @since 1.0.6
     */
    private List<String> filterInnerDriver(List<String> driverIds) {
        //@todo
        return driverIds;
    }

    /**
     * @param driverIds
     * @param points
     * @author Anson
     * @date 17-8-10
     * @name 排除坐标中不含有的司机
     * @since 1.0.6
     */
    private BusPoint[] matcherDriverIds(List<String> driverIds, BusPoint[] points) {
        if (ArrayUtils.isEmpty(points) || CollectionUtils.isEmpty(driverIds)) {
            return new BusPoint[0];
        }
        List<BusPoint> list = Arrays.asList(points);
        //过滤出真实的车辆坐标过滤
        CollectionUtils.filter(list, new Predicate<BusPoint>() {
            @Override
            public boolean evaluate(BusPoint object) {
                String driverId = object.getDriverId();
                return (!StringUtils.isEmpty(driverId)) && driverIds.contains(driverId);
            }
        });
        return list.toArray(new BusPoint[list.size()]);
    }

}
